<template>
  <div class="app-container">
    <!-- 头部搜索 -->
   <div class="filter-container">
      <el-select v-model="selectValue"  placeholder="请选择" class='inp_search'>
          <el-option
          v-for="item in optionsData"
          :key="item.value"
          :label="item.label"
          :value="item.value">
          </el-option>
      </el-select>
      
      <el-input
          placeholder="输入查询"
          v-model="input_conten"
          clearable class='inp_search'>
      </el-input>

      <el-button  class="filter-item" type="primary" icon="el-icon-search" @click="searchList">
        搜索
      </el-button>
      <el-button v-if="role == '4'"  class="filter-item" type="success" icon="el-icon-plus" @click="addStock('add','ruleForm')">
        添加
      </el-button>
      <el-button  class="filter-item" type="primary" icon="el-icon-refresh"  @click="resList">
        重置
      </el-button>
    </div>
     
     <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >

      <el-table-column label="工具ID" width="120" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.toolId === null">--</span>
          <span v-else> {{scope.row.toolId}} </span>
        </template>
      </el-table-column>

      <el-table-column label="货位" min-width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.location === null">--</span>
          <span v-else> {{scope.row.location}} </span>
        </template>
      </el-table-column>

      <el-table-column label="工具名称" min-width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.toolName === null">--</span>
          <span v-else> {{scope.row.toolName}} </span>
        </template>
      </el-table-column>

      <el-table-column label="型号" min-width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.model === null">--</span>
          <span v-else> {{scope.row.model}} </span>
        </template>
      </el-table-column>

      <el-table-column label="品牌" min-width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.brand === null">--</span>
          <span v-else> {{scope.row.brand}} </span>
        </template>
      </el-table-column>

      <el-table-column label="单价" min-width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.price === null">--</span>
          <span v-else> {{scope.row.price}} </span>
        </template>
      </el-table-column>

      <el-table-column label="剩余数量" min-width="200" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.quantity === null">--</span>
          <span v-else> {{scope.row.quantity}} </span>
        </template>
      </el-table-column>

      <el-table-column v-if="role == '4'"  label="操作" width="300" align="center"  fixed="right">
        <template scope="scope">
					<el-button type="primary" size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDel(scope.$index, scope.row,'ruleForm')">删除</el-button>
				</template>
      </el-table-column>
    </el-table>
   <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" 
     @pagination="fetchData"  />

   <!-- 添加 -->
   <div class='formPosition' v-if='dialogFormVisible'>
   <div class='titleForm'>
          <div class='left_titleForm'>{{titleName}}</div>
          <span class="icon iconfont right_titleForm" @click="resetForm('ruleForm')">&#xe72d;</span>
   </div>
   <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm " :visible.sync="dialogFormVisible">
         
          <el-form-item label="工具名称" prop="toolName">
              <el-input v-model="ruleForm.toolName"></el-input>
          </el-form-item>
          
          <el-form-item v-if="this.titleName === '修改'" label="ID" prop="toolId">
          <el-input readonly v-model="ruleForm.toolId"></el-input>
          </el-form-item>

          <el-form-item label="货位" prop="location">
          <el-input v-model="ruleForm.location"></el-input>
          </el-form-item>

          <el-form-item label="品牌" prop="brand">
          <el-input v-model="ruleForm.brand"></el-input>
          </el-form-item>

          <el-form-item label="型号" prop="model">
          <el-input v-model="ruleForm.model"></el-input>
          </el-form-item>

          <el-form-item label="数量" prop="quantity">
          <el-input v-model.number="ruleForm.quantity"></el-input>
          </el-form-item>

          <el-form-item label="单价" prop="price">
          <el-input v-model.number="ruleForm.price"></el-input>
          </el-form-item>

          <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>

     </el-form>
    </div>
  </div>
   
   
  
</template>

<script>
import { getBillManage ,modifyBillManage ,searchByPillId} from '@/api/billManages'
import { getToolsList , addToolsList , searchByName ,searchByID ,modifyByID ,delByID} from '@/api/stockManage'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data() {
    return {
      role:'',
      input_conten:'',
      optionsData: [{
          value: 'stockId',
          label: '工具ID'
        },{
          value: 'stockName',
          label: '工具名称'
        },],
      selectValue: '',
      titleName:'',
      list: null,
      total:0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
      },
      dialogFormVisible:false,
      ruleForm: {
          toolName:'',
          quantity:'',
          brand:'',
          model:'',
          location:'',
          price:'',
          toolId:''
        },
      cleanForm: {
          toolName:'',
          quantity:'',
          brand:'',
          model:'',
          location:'',
          price:'',
          toolId:''
        },
        rules: {
          toolName: [
            { required: true, message: '请输入工具名称', trigger: 'blur' },
          ],
          quantity: [
            {type: 'number',required: true, message: '请输入正确格式的数量', trigger: 'blur' }
          ],
          brand: [
            { required: true, message: '请输入品牌', trigger: 'blur' }
          ],
          model: [
            { required: true, message: '请输入型号', trigger: 'blur' }
          ],
          location: [
            { required: true, message: '请输入货柜', trigger: 'blur' }
          ],
          price: [
            {type: 'number',required: true, message: '请输入正确格式单价', trigger: 'blur' }
          ]
        }
    }
  },
  created() {
    this.fetchData()//获取列表数据
    //初始化设置搜索下拉框的值
    const userInfo = localStorage.getItem('userInfo');
    this.role = JSON.parse(userInfo).role;
    this.selectValue = this.optionsData[0].label;
  },
  methods: {
    fetchData() {
       this.listLoading = true
       const paramsData = {curPage:this.listQuery.page,pageSize:this.listQuery.limit}
       getToolsList(paramsData).then(res => {
        this.list = res.data.obj;
        this.total = res.data.countNum;
        this.listLoading = false
      })
    },
   submitForm(formName) {//提交
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.listLoading = true
            if(this.titleName === "添加"){
               this.handleAdd(formName)
            }else{
               this.handleModify(formName)
            }
            this.listLoading = false;
            this.resetForm(formName) 
          } else {
            this.$notify({
                    message: '提交失败',
                    type: 'error',
                    duration: 2000
                })
            return false;
          }
        });
      },
      handleAdd(formName){//添加
            const paramsData = {
                toolName:this.ruleForm.toolName,
                quantity:this.ruleForm.quantity,
                price:this.ruleForm.price,
                brand:this.ruleForm.brand,
                model:this.ruleForm.model,
                location:this.ruleForm.location
                }
              addToolsList(paramsData).then(res => {
                this.list = res.data;
                if(res.code == '200'){
                        this.$notify({
                        message: '添加成功',
                        type: 'success',
                        duration: 2000
                    })
                  }else{
                    this.$notify({
                        message: '添加失败',
                        type: 'error',
                        duration: 2000
                    })
                  }
                  this.fetchData()//获取列表
            })
      },
      handleModify(formName){//修改
             const paramsData = {
                  toolId:this.ruleForm.toolId,
                  toolName:this.ruleForm.toolName,
                  quantity:this.ruleForm.quantity,
                  price:this.ruleForm.price,
                  brand:this.ruleForm.brand,
                  model:this.ruleForm.model,
                  location:this.ruleForm.location
                  }
                modifyByID(paramsData).then(res => {
                  this.list = res.data;
                  if(res.code == '200'){
                        this.$notify({
                        message: '修改成功',
                        type: 'success',
                        duration: 2000
                    })
                  }else{
                    this.$notify({
                        message: '修改失败',
                        type: 'error',
                        duration: 2000
                    })
                  }
                  this.fetchData()//获取列表
              })
      },
      resetForm(formName) {//清除弹出框默认值
          this.ruleForm = this.cleanForm;
          this.$refs.ruleForm.resetFields();
          this.dialogFormVisible = false;     
      },
      addStock(formName){//处理弹出框添加和修改的状态
          this.dialogFormVisible = true;     
          this.titleName = '添加'
      },
      handleEdit(index, row ) {//修改
          this.titleName = '修改'
          this.dialogFormVisible = true;
          this.ruleForm = Object.assign({}, row);
      },
      handleDel(index, row ,formName){//删除
           this.ruleForm = Object.assign({}, row);
           const paramsData = { toolId:this.ruleForm.toolId}
                delByID(paramsData).then(res => {
                  this.ruleForm = this.cleanForm;
                  if(res.code == '200'){
                    if (this.total % this.listQuery.limit === 1) {    // 表格总数量 % 行数  余出的就是当前页有几个
                        const lastPage = (this.total + this.listQuery.limit - 1) / this.listQuery.limit  // （表格总数量 +行数 -1) / 行数
                        if (this.listQuery.page === lastPage) { // 当前页 === （表格总数量 +行数 -1) / 行数
                          this.listQuery.page = this.listQuery.page - 1 // 减去一页就是前一页
                        }
                      }
                        this.$notify({
                        message: '删除成功',
                        type: 'success',
                        duration: 2000
                    })
                  }else{
                    this.$notify({
                        message: '删除失败',
                        type: 'error',
                        duration: 2000
                    })
                  }
                  this.fetchData()//获取列表
              })
      },
      resList(){
          this.fetchData()//获取列表数据
          //初始化设置搜索下拉框的值
          const userInfo = localStorage.getItem('userInfo');
          this.role = JSON.parse(userInfo).role;
          this.selectValue = this.optionsData[0].label;
      },
      searchList(){//搜索
          if(this.input_conten){
              if(this.selectValue == "stockName"){//按工具名称搜索
                  const paramsData = {toolName:this.input_conten,curPage:this.listQuery.page,pageSize:this.listQuery.limit}
                  searchByName(paramsData).then(res => {
                      this.list = res.data.obj
                  })
              }else{//按工具id搜索
                  const paramsData = {toolId:this.input_conten}
                  searchByID(paramsData).then(res => {
                      this.list = res.data.obj
                  }) 
              }
          }else{
              this.fetchData();
              // this.$notify({
              //       message: '请先输入搜索内容',
              //       type: 'error',
              //       duration: 2000
              //   })
              return false;
          }
      }

    }
  
 }
</script>
<style lang="scss">
 .filter-container{
   margin-bottom:20px;
 }
 .el-date-range-picker{
   margin-left:220px;
 }
 .inp_search{
   width:20%;
   margin-right:1%;        
 }
 .formPosition{
    width:70%;
    height:auto;
    background:#fff;
    position:absolute;
    left:15%;
    top:5%;
    padding:30px;
    box-shadow: 0px 0px 5px 5px #eee;
    border-radius: 5px;
 }
 .demo-ruleForm{
      width:50%;     
 }
 .titleForm{
     width:100%;
     height:30px;
     line-height:30px;
     margin-bottom:20px;
     font-size:1.1em;
     font-weight: 500;
 }
 .left_titleForm{
    float:left;
 }
 .right_titleForm{
    display:block;
    float:right;
 }
 .select_roles{
    width:100%;
 }
</style>
